Shader "Custom/UIRoundCorner"
{
	Properties
	{
        [PerRendererData] _MainTex("Main Texture", 2D) = "white" {}
        _Color("Tint", Color) = (1, 1, 1, 1)

        _StencilComp("Stencil Comparison", Float) = 8
        _Stencil("Stencil ID", Float) = 0
        _StencilOp("Stencil Operation", Float) = 0
        _StencilWriteMask("Stencil Write Mask", Float) = 255
        _StencilReadMask("Stencil Read Mask", Float) = 255
        _ColorMask("Color Mask", Float) = 15
        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 1

        [Header(UserSetting)]
        [Space]
        [Toggle(ANTI_ALIASING)] _AntiAliasing ("AntiAliasing", Float) = 1
        [Toggle(ROUND_CORNER_LEFT_TOP)] _RoundCornerLeftTop ("RoundCornerLeftTop", Float) = 1
        [Toggle(ROUND_CORNER_LEFT_BOTTOM)] _RoundCornerLeftBottom ("RoundCornerLeftBottom", Float) = 1
        [Toggle(ROUND_CORNER_RIGHT_TOP)] _RoundCornerRightTop ("RoundCornerRightTop", Float) = 1
        [Toggle(ROUND_CORNER_RIGHT_BOTTOM)] _RoundCornerRightBottom ("RoundCornerRightBottom", Float) = 1
        _RoundCornerRange("Round Corner Range", float) = 20
        _Edge("Edge", float) = 0
        _RectSize("Rect Size", Vector) = (100, 100, 0, 0)
        _DistanceOffset("DistanceOffset", Range(0,1)) = 0.8
        [Toggle(ONLY_ONEFIFTH_BOTTOM)] _OpenOnlyOneFifth_Bottom("OnlyOne-FifthBottom", Float) = 0
        [Toggle(ONLY_FIVEFIFTH_BOTTOM)] _OpenOnlyfiveFifth_Bottom("Onlyfive-FifthBottom", Float) = 0
        [Toggle(ONLY_CENTER_CIRCLE)] _OpenOnlyCenterCircle("OnlyCenterCircle", Float) = 0

        [Space]
        _HighLight("HighLight", Float) = 0
	}
    SubShader
    {
        LOD 100
        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType" = "Plane"
            "CanUseSpriteAtlas" = "True"
        }

        Stencil
        {
            Ref[_Stencil]
            Comp[_StencilComp]
            Pass[_StencilOp]
            ReadMask[_StencilReadMask]
            WriteMask[_StencilWriteMask]
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask[_ColorMask]

        Pass
        {
            Name "Default"
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
            #pragma multi_compile_local _ UNITY_UI_ALPHACLIP

            #pragma multi_compile_local _ ANTI_ALIASING
            #pragma multi_compile_local _ ROUND_CORNER_LEFT_TOP
            #pragma multi_compile_local _ ROUND_CORNER_LEFT_BOTTOM
            #pragma multi_compile_local _ ROUND_CORNER_RIGHT_TOP
            #pragma multi_compile_local _ ROUND_CORNER_RIGHT_BOTTOM
            #pragma multi_compile_local _ ONLY_ONEFIFTH_BOTTOM
            #pragma multi_compile_local _ ONLY_FIVEFIFTH_BOTTOM
            #pragma multi_compile_local _ ONLY_CENTER_CIRCLE


            struct appdata_t
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                float2 uv : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            fixed4 _MainTex_ST;
            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            fixed4 _ClipRect;
            fixed _RoundCornerRange;
            fixed _Edge;
            float2 _RectSize;
            float _HighLight;
            float _DistanceOffset;


            v2f vert(appdata_t v)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.worldPosition = v.vertex;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                OUT.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                OUT.color = v.color * _Color;
                return OUT;
            }


            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 color = (tex2D(_MainTex, i.uv) + _TextureSampleAdd) * i.color;

                #ifdef UNITY_UI_CLIP_RECT
                color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
                #endif

                float roundCornerRange = min(_RoundCornerRange, min(_RectSize.x, _RectSize.y) / 2);
                roundCornerRange = max(0, roundCornerRange);
                float2 srcUV = i.uv;
                float edge = _Edge;
                if(edge < 1)
                    edge = max(_RectSize.x, _RectSize.y);

                i.uv *= _RectSize;
                float distance = 1;
                bool corner = false;

                // 削外部圆角
                #ifdef ROUND_CORNER_LEFT_BOTTOM
                if (i.uv.x < roundCornerRange && i.uv.y < roundCornerRange) {
                    distance = pow(i.uv.x - roundCornerRange, 2) + pow(i.uv.y - roundCornerRange, 2);
                    corner = true;
                }
                #endif
                #ifdef ROUND_CORNER_LEFT_TOP
                if (i.uv.x < roundCornerRange && i.uv.y >_RectSize.y - roundCornerRange) {
                    distance = pow(i.uv.x - roundCornerRange, 2) + pow(i.uv.y - (_RectSize.y - roundCornerRange), 2);
                    corner = true;
                }
                #endif
                #ifdef ROUND_CORNER_RIGHT_TOP
                if (i.uv.x >_RectSize.x - roundCornerRange && i.uv.y >_RectSize.y - roundCornerRange) {
                    distance = pow(i.uv.x - (_RectSize.x - roundCornerRange), 2) + pow(i.uv.y - (_RectSize.y - roundCornerRange), 2);
                    corner = true;
                }
                #endif
                #ifdef ROUND_CORNER_RIGHT_BOTTOM
                if (i.uv.x >_RectSize.x - roundCornerRange && i.uv.y < roundCornerRange) {
                    distance = pow(i.uv.x - (_RectSize.x - roundCornerRange), 2) + pow(i.uv.y - roundCornerRange, 2);
                    corner = true;
                }
                #endif

                if(corner){
                    distance = roundCornerRange - sqrt(distance);
                    distance+= _DistanceOffset;
                    if(distance >= 1)
                    {
                        distance = edge - distance - (1 - _DistanceOffset);
                    }
                }
                else if(edge > max(_RectSize.x, _RectSize.y) / 2)
                {
                    float distance_x = min(i.uv.x, abs(i.uv.x - _RectSize.x));
                    float distance_y = min(i.uv.y, abs(i.uv.y - _RectSize.y));
                    distance = min(distance_x, distance_y)+1;
                }
                else
                {
                    float distance_x = 0;
                    float distance_y = 0;

                    if (i.uv.x < edge) {
                        distance_x = abs(i.uv.x - edge);
                    }
                    else if (i.uv.x >_RectSize.x - edge) {
                        distance_x = abs(i.uv.x - (_RectSize.x - edge));
                    }

                    if (i.uv.y <edge) {
                        distance_y = abs(i.uv.y - edge);
                    }
                    else if (i.uv.y >_RectSize.y - edge) {
                        distance_y = abs(i.uv.y - (_RectSize.y - edge));
                    }

                    distance = min(max(distance_x, distance_y) - 1, min(min(i.uv.x, i.uv.y) + _DistanceOffset, min(_RectSize.x - i.uv.x, _RectSize.y - i.uv.y) + _DistanceOffset));
                }
                #ifdef ANTI_ALIASING
                    distance = clamp(distance,0, 1);
                #else
                    distance = step(0.5, distance);
                #endif
                color.a *= distance;
                color.rgb *= 1 + _HighLight;

                #ifdef ONLY_ONEFIFTH_BOTTOM
                color.a *= step(srcUV.y, 0.2);
                #endif

                #ifdef ONLY_FIVEFIFTH_BOTTOM
                color.a *= step(0.2,srcUV.y);
                #endif

                #ifdef ONLY_CENTER_CIRCLE
                float centerCircleEdge = roundCornerRange - edge + 2;
                if (centerCircleEdge > 0)
                {
                    float centerDistanceX = i.uv.x - _RectSize.x * 0.5;
                    float centerDistanceY = i.uv.y - _RectSize.y * 0.5;
                    color.a = sqrt(centerDistanceX * centerDistanceX + centerDistanceY * centerDistanceY) < centerCircleEdge ? 1 - color.a : 0;
                }
                else {
                    color.a = color.a;
                }
                #endif

                #ifdef UNITY_UI_ALPHACLIP
                    clip(color.a - 0.001);
                #endif

                return color;
            }
            ENDCG
        }
    }
}
